package cn.edu.nuc.ssm.service.impl;

import cn.edu.nuc.ssm.dao.AdminMapper;
import cn.edu.nuc.ssm.entity.Admin;
import cn.edu.nuc.ssm.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by Administrator on 2017/5/3.
 */
@Service
public class AdminServiceImpl implements AdminService{

    @Autowired
    private AdminMapper adminMapper;

    @Override
    public Admin login(String username, String password) {

        List<Admin> admins = adminMapper.findbyUsernameandpassword(username, password);

        if (admins.size() <= 0||admins.size() >1){

            return null;
        }
        return admins.get(0);
    }

    public Admin findById(Integer id){

        Admin admin = adminMapper.findbyId(id);
        return admin;
    }

    @Override
    public int updatePasswordByid(Integer id, String password) {

        return adminMapper.updatePasswordbyId(password,id);
    }

    @Override
    public int insert(Admin admin) {

        return adminMapper.insert(admin);
    }

    @Override
    public Admin findbyUsername(Admin admin) {

        List<Admin> admins = adminMapper.findbyUsername(admin.getUsername());

        if (admins.size()<=0){

            return null;
        }

        return admins.get(0);
    }

    @Override
    public List<Admin> find() {

        return adminMapper.find();
    }

    @Override
    public int update(Admin admin) {

        return adminMapper.updateByPrimaryKey(admin);
    }

    @Override
    public int delete(int id) {

        return adminMapper.deleteByPrimaryKey(id);
    }

    public List<Admin> listAllAdmin(){


        List<Admin> adminList = adminMapper.find();
        return adminList;
    }
}
